When we create variable it is nothing but reserved memory locations to store values. This means when you create variable you reserve memory space,now the operating system allocates memory and determines what can be put in the reserved memory based on
a variable's data type. You may thus store integers, decimals, or characters in variables by giving them alternative data types.
In Java, there are two categories of data types:
-
Premetive Data Type Data types that are considered primitive include booleans, chars, bytes, short ints, long ints, floats, and doubles.
-
Non Premetive Data Type Non-primitive data Classes, Interfaces, and Arrays are examples of non-primitive data types.
In java all variables must be declared before its use. Because java is a statically-typed programming language that is why we need to declare variable's type and name
Types of Data Type
In Java we have 8 different type of premitive data type
-
boolean data type
-
byte data type
-
char data type
-
short data type
-
int data type
-
long data type
-
float data type
-
double data type
S.N |
Data Type |
Default Value |
Default size |
1. |
boolean |
false |
1 bit |
2. |
byte |
0 |
1 byte |
3. |
char |
'\u0000' |
2 byte |
4. |
short |
0 |
2 byte |
5. |
int |
0 |
4 byte |
6. |
long |
0L |
8 byte |
7. |
float |
0.0f |
4 byte |
8. |
double |
0.0d |
8 byte |
Byte
-
byte data type is a basic data type in java
-
It is an integer with two's complement and an 8-bit sign.
-
Its value range between -128 to 127 (inclusive).
-
byte data type minimum and maximum values are -128 and 127.
-
When memory optimization are most needed, such in huge arrays, the byte data type is used because a byte is four times smaller than an integer, thus space is saved .
-
The "int" data type can likewise be replaced with this byte data type.
Example:
byte a = 10, byte b = -20;
Boolean Data Type
-
Only the true and false values are stored using the Boolean data type. This data type is utilised for straightforward true/false flags.
-
The Boolean data type represents a single piece of data, however its exact "size" cannot be determined.
Example:
Boolean a = false
Short Data Type
-
Short data type is 16-bit signed two's complement integer.
-
Its range of values ranges from -32,768 to 32,767.
-
The default value is zero (0).
Example:
short s = 10000, short r = -5000
Int Data Type
-
Int data type is a 32-bit signed two's complement integer.
-
Its value ranges between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1), respectively (inclusive).
-
2,147,483,648 is its smallest value and 2,147,483,647 is its highest value.
-
The default value is 0.
-
Unless there is no memory issue, the int data type is often chosen as the default data type for integral values.
Example:
int s = 100, int r = 500
Long Data Type
-
Lond data type is a two's complement, 64-bit long data type.
-
Between -9,223,372,036,854,775,808(-2^63) and 9,223,372,036,854,775,807(2^63 -1) are its possible values (inclusive).
-
Its minimum and maximum value is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 respectively.
-
The default value is 0.
-
When you want a wider range of values than int, the long data type is used
Example:
long s = 10000L, long r = -200000L
Float Data Type
-
A single-precision 32-bit IEEE 754 floating point is the float data type.
-
It has an infinite value range.
-
If you need to save memory in sizable arrays of floating point integers, it is advised to use a float (instead of a double).
-
Never use the float data type for accurate numbers like cash.
-
The default value is 0.0F.
Example:
Integer s = 195.5f
Double Data Type
-
A double-precision 64-bit IEEE 754 floating point is the double data type.
-
It has an infinite value range.
-
Like float, the double data type is frequently used for decimal numbers.
-
Additionally, it's never a good idea to utilise the double data type for exact numbers like money.
-
The default value is 0.0d.
Char Data Type
-
char data type is 16-bit Unicode character.
-
Its value range is from '\u0000'(0) to '\uffff' (65,535)
-
We can store character using char data type.
Example:
character letter = "A"
Unicode System
The majority of the world's written languages can be represented using the universal international standard character encoding known as Unicode.
Unicode System in Java
A code that converts characters into numbers must exist in order for a computer system to store text and numbers that humans can understand.
A character encoding standard called Unicode is used to define the appropriate code. The process of allocating a number to each character is known as character encoding. The primary goal of Unicode is to standardise various
language encoding systems in order to reduce confusion among computer systems that use restrictive encoding standards like ASCII.
Why java uses Unicode System?
The Unicode standards were developed at a period when Java was evolving and had a much smaller character set. When the Unicode Transformed Format (UTF)-16 was created, Java was meant to use it. Java's "char" data type was
initially utilised to represent 16-bit Unicode. Java therefore uses the Unicode standard.
-
Before Unicode there where many language standers like ASCII (American Standard Code for Information Interchange) for the United States.
-
ISO 8859-1 for Western European Language.
-
KOI-8 for Russian.
-
GB18030 and BIG-5 for Chinese
Two issues resulted from this:
-
In many linguistic standards, certain letters are assigned to a specific code value.
-
Variable length encodings are used for languages with vast character sets.
-
Some frequently used characters may be encoded in a single byte, while others need two or more.
-
A new language standard called the Unicode System was created to address these issues.
-
Since each character in unicode takes up two bytes, Java also uses two bytes for each character.
-
lowest value: u0000
highest value: uFFFF
Post your comment